﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _06_04_单窗体实现增删改查
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BindData()
        {
            //连接数据库
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(conn);
            sqlconn.Open();
            string sql = "select * from Member";
            SqlDataAdapter adp = new SqlDataAdapter(sql, sqlconn);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            this.dataGridView1.DataSource = dt;

            sqlconn.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(conn);
            sqlconn.Open();
            string cmd = $"insert into Member(MemberAccount,MemberPwd,MemberName,MemberPhone) " +
                $"values('{this.textBox1.Text}','{this.textBox2.Text}','{this.textBox3.Text}','{this.textBox4.Text}')";
            SqlCommand sqlcmd = new SqlCommand(cmd, sqlconn);
            int count = sqlcmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            BindData();
            sqlconn.Close();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            string sql = $"delete   from Member where MemberId=" +
                $" '{this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString()}'";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlConnection);
            int count = sqlcmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            sqlConnection.Close();
            this.BindData();
        }

        private void dataGridView1_Click(object sender, DataGridViewCellEventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            string sql = "select * from Member where MemberId=" + id;
            SqlDataAdapter adp = new SqlDataAdapter(sql, sqlConnection);
            DataTable dt = new DataTable();//查询的数据存入内存
            adp.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("查询失败");
                this.BindData();
                return;
            }
            this.textBox1.Text = dt.Rows[0]["MemberAccount"].ToString();
            this.textBox2.Text = dt.Rows[0][1].ToString();
            this.textBox3.Text = dt.Rows[0][2].ToString();
            this.textBox4.Text = dt.Rows[0][3].ToString();

            string sqlup = "update Member set MemberAccount";
            sqlConnection.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            string sql = $"update Member set MemberAccount ='{this.textBox1.Text}'," +
                $" MemberPwd='{this.textBox2.Text}',MemberName='{this.textBox3.Text}',MemberPhone='{this.textBox4.Text}'" +
                $"where MemberId={id}";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlConnection);
            if (sqlcmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("更新成功");
                this.textBox1.Text ="";
                this.textBox2.Text = "";
                this.textBox3.Text = "";
                this.textBox4.Text = "";
                this.BindData();
            }
            else
            {
                MessageBox.Show("更新失败");
            }
            sqlConnection.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
